package com.jiangxjsy.crm.support;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiangxjsy.core.constant.BasicConstant;
import com.jiangxjsy.core.enums.FieldEnum;
import com.jiangxjsy.crm.service.CrmFieldExtendService;
import com.jiangxjsy.model.crm.bo.CrmFieldBO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/18 16:57
 */

@Component
public class CrmFieldSupport {
    @Resource
    private CrmFieldExtendService crmFieldExtendService;

    /**
     * 将record转成formType2
     *
     * @param record record
     * @param typeEnum typeEnum
     */
    public void recordToFormType2(CrmFieldBO record, FieldEnum typeEnum) {
        record.setFormType(typeEnum.getFormType());
        switch (typeEnum) {
            case CHECKBOX:
                record.setDefaultValue(StrUtil.splitTrim((CharSequence) record.getDefaultValue(), BasicConstant.SEPARATOR));
            case SELECT:
                if(Objects.equals(record.getRemark(),FieldEnum.OPTIONS_TYPE.getFormType())) {
                    JSONObject optionsData = JSON.parseObject(record.getOptions());
                    record.setOptionsData(optionsData);
                    record.setSetting(new ArrayList<>(optionsData.keySet()));
                }else {
                    record.setSetting(StrUtil.splitTrim(record.getOptions(), BasicConstant.SEPARATOR));
                }
                break;
            case DATE_INTERVAL:
                String dataValueStr = Optional.ofNullable(record.getDefaultValue()).orElse("").toString();
                record.setDefaultValue(StrUtil.split(dataValueStr, BasicConstant.SEPARATOR));
                break;
            case USER:
            case STRUCTURE:
                record.setDefaultValue(new ArrayList<>(0));
                break;
            case AREA:
            case AREA_POSITION:
            case CURRENT_POSITION:
                String defaultValue = Optional.ofNullable(record.getDefaultValue()).orElse("").toString();
                record.setDefaultValue(JSON.parse(defaultValue));
                break;
            case DETAIL_TABLE:
                record.setFieldExtendList(crmFieldExtendService.queryCrmFieldExtend(Math.toIntExact(record.getFieldId())));
                break;
            default:
                record.setSetting(new ArrayList<>());
                break;
        }
    }

    /**
     * 判断字段类型是否相等
     *
     * @param type 字段类型
     * @param fieldEnums 字段枚举
     * @return 是否相等
     */
    public boolean equalsByType(Object type, FieldEnum... fieldEnums){
        if (type instanceof String){
            for (FieldEnum anEnum : fieldEnums) {
                if(anEnum.getFormType().equals(type)){
                    return true;
                }
            }
        }else {
            for (FieldEnum anEnum : fieldEnums) {
                if (Objects.equals(anEnum.getType(),type)) {
                    return true;
                }
            }
        }
        return false;
    }
}
